package com.feng.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.feng.commonutils.JwtUtils;
import com.feng.commonutils.MD5;
import com.feng.commonutils.R;
import com.feng.educenter.entity.UcenterMember;
import com.feng.educenter.mapper.UcenterMemberMapper;
import com.feng.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.educenter.utils.BeanCopyUtils;
import com.feng.educenter.vo.RegisterVo;
import com.feng.servicebase.excecption.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author fengge
 * @since 2023-03-26
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    /**
     * 登录功能的实现
     */
    @Autowired
    private RedisTemplate<String,String > redisTemplate;
    @Override
    public R login(UcenterMember ucenterMember) {
        //获取手机号
        String mobile = ucenterMember.getMobile();
        //获取密码
        String password = ucenterMember.getPassword();
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001,"手机号或密码不能为空");

        }
        //判断手机号是否正确
        LambdaQueryWrapper<UcenterMember> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(UcenterMember::getMobile,ucenterMember.getMobile());
        UcenterMember one = this.getOne(queryWrapper);
        //判断该对象是否为空
        if(ObjectUtils.isEmpty(one)){
          //没有这个手机号
            throw new GuliException(20001,"手机号不存在，请先注册");
        }
        //判断密码,加密方式MD5
        if(!MD5.encrypt(password).equals(one.getPassword())){
            throw new GuliException(20001,"手机号或密码错误");
        }
        //判断是否被禁用
        if(one.getIsDisabled()){
            throw new GuliException(20001,"该账号已注销");
        }
        //登录成功，生成一个token，使用jwt工具
        String token = JwtUtils.getJwtToken(one.getId(), one.getNickname());
        return R.ok().data("token",token);
    }

    @Override
    public R registerUser(RegisterVo registerVo) {
        //获取注册的数据
        //验证码
        String code = registerVo.getCode();
        //手机号
        String mobile = registerVo.getMobile();
        //昵称
        String nickname = registerVo.getNickname();
        //密码
        String password = registerVo.getPassword();
        //非空判断
        if (StringUtils.isEmpty(nickname)
                ||StringUtils.isEmpty(code)
                ||StringUtils.isEmpty(mobile)
                ||StringUtils.isEmpty(password)){
            throw new GuliException(20001,"传来的数据有空值，注册失败");
        }
        //判断手机号是否重复，如果相同不添加
        LambdaQueryWrapper<UcenterMember> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(UcenterMember::getMobile,registerVo.getMobile());
        UcenterMember one = this.getOne(queryWrapper);
        if(!ObjectUtils.isEmpty(one)){
            throw new GuliException(20001,"手机号已存在");
        }
        //判断验证码和存的验证码是否一样
        String redisCode = redisTemplate.opsForValue().get(mobile);
        //验证码已过期
        if(StringUtils.isEmpty(redisCode)){
            throw new GuliException(20001,"验证码已过期");
        }
        if(!code.equals(redisCode)){
            throw new GuliException(20001,"验证码错误");
        }

        //数据库的添加
        UcenterMember ucenterMember = BeanCopyUtils.copyBean(registerVo, UcenterMember.class);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setIsDisabled(false);
        ucenterMember.setAvatar("https://edu-202323.oss-cn-guangzhou.aliyuncs.com/2023/03/15/8a614e24ab834ca0bad5b529ed84cec8file.png");
        this.save(ucenterMember);
         return R.ok();


    }

    @Override
    public R getMemberInfo(HttpServletRequest httpServletRequest) {
        //调用jwt工具类方法，根据request对象获取头信息，返回用户id
        String id = JwtUtils.getMemberIdByJwtToken(httpServletRequest);
        //查询数据库根据用户id获取用户信息
        UcenterMember member = this.getById(id);
        member.setPassword("");
        return R.ok().data("userInfo",member);
    }

    @Override
    public UcenterMember getOpenIdMember(String oepnid) {
        LambdaQueryWrapper<UcenterMember> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(UcenterMember::getOpenid,oepnid);
        UcenterMember ucenterMember = this.getOne(queryWrapper);
        return ucenterMember;
    }

    @Override
    public Integer countRegister(String day) {
        return baseMapper.countRegister(day);
    }
}
